/*
 * Copyright 2009 SHOP.COM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shop.opensocial.mediated.util;

import com.shop.opensocial.mediated.types.OAuthConsumerMediator;
import com.shop.opensocial.mediated.types.SecurityTokenMediator;
import com.shop.opensocial.mediated.types.OAuthServiceProviderMediator;
import com.shop.opensocial.mediated.services.AuthenticationHandlerMediator;
import com.shop.opensocial.mediated.services.OAuthStoreMediator;
import com.shop.opensocial.mediated.request.MediatedRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * Untility to validate OAuth requests
 *
 * @author Jordan Zimmerman
 */
public interface OAuthValidator
{
	/**
	 * POJO returned by {@link OAuthValidator#isValidOAuthQuery(HttpServletRequest, OAuthConsumerMediator)}
	 */
	public static class OAuthValidatorResult
	{
		/**
		 * If non-null, request is invalid and "error" is the reason. If null, the request is valid.
		 */
		public final String				error;

		/**
		 * the Owner ID from the request (if the request is valid)
		 */
		public final String				ownerId;

		/**
		 * the Viewer ID from the request (if the request is valid)
		 */
		public final String				viewerId;

		public OAuthValidatorResult(String error, String ownerId, String viewerId)
		{
			this.error = error;
			this.ownerId = ownerId;
			this.viewerId = viewerId;
		}
	}

	/**
	 * Use to validate the OAuth parameters on the server side of a makeRequest() call
	 *
	 * @param request the HTTP request
	 * @param consumer the consumer to validate against. You can pass null and {@link #getConsumer(HttpServletRequest)} will be used.
	 * @return validation POJO
	 * @throws Exception errors
	 */
	public OAuthValidatorResult 	isValidOAuthQuery(HttpServletRequest request, OAuthConsumerMediator consumer) throws Exception;

	/**
	 * Returns the consumer key from the given request
	 *
	 * @param request the HTTP request
	 * @return the Consumer Key if any
	 * @throws Exception errors
	 */
	public String					getConsumerKey(HttpServletRequest request) throws Exception;

	/**
	 * Returns the consumer represented by the given request. Internally, {@link AuthenticationHandlerMediator#getSecurityTokenFromRequest(HttpServletRequest)}
	 * is called and then {@link OAuthStoreMediator#getConsumerKeyAndSecret(MediatedRequest, SecurityTokenMediator, String, OAuthServiceProviderMediator)} is called.
	 *
	 * @param request the HTTP request
	 * @return the Consumer or null
	 * @throws Exception errors
	 */
	public OAuthConsumerMediator	getConsumer(HttpServletRequest request) throws Exception;
}
